const path = require('path');
const VueLoaderPlugin = require('vue-loader/lib/plugin')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
// const CopyPlugin = require("copy-webpack-plugin");
const webpack = require('webpack')

/** @type { import('webpack').Configuration } */
module.exports = {
  mode: 'none',
  entry: './src/main.js',
  devtool: 'eval-cheap-module-source-map',
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'bundle.js',
  },
  module: {
    rules: [
      {
        test: /\.vue$/,
        use: [
          'vue-loader'
        ]
      },
      {
        test: /\.js$/,
        loader: 'babel-loader'
      },
      {
        test: /\.less$/,
        use: [
          'style-loader',
          'css-loader',
          'less-loader',
        ]
      },
      {
        test: /\.css$/,
        use: [
          'style-loader',
          'css-loader'
        ]
      },
      {
        test: /\.(gif|png|jpe?g|svg)$/i,
        use: [
          {
            loader: 'file-loader',
            options: {
              esModule: false,  // 关闭后 使得.vue文件中的 img 标签可以打包
            },
          },
        ],
      },
    ]
  },
  plugins: [
    new CleanWebpackPlugin(),
    new VueLoaderPlugin(),
    new webpack.DefinePlugin({
      BASE_URL: JSON.stringify('./'),
      'process.env': {
        NODE_ENV: JSON.stringify(process.env.NODE_ENV)
      },
    }),
    // new CopyPlugin({
    //   patterns: [
    //     { from: "./public",
    //       to: "",
    //       filter: (resourcePath) => {
    //         if(/\.html$/.test(resourcePath)){
    //           return false;
    //         }
    //         return true;
    //       }
    //     },
    //   ],
    // }),
    new HtmlWebpackPlugin({
      title: 'Webpack打包 vue 项目',
      template: './public/index.html',
    }),
  ]
}
